package com.java.service.impl;

import com.java.entity.*;
import com.java.mapper.QuestionOptionMapper;
import com.java.mapper.custom.CustomPaperMapper;
import com.java.service.PaperService;
import com.java.service.TeacherService;
import com.java.util.ResultVo;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PaperServiceImpl implements PaperService {

    @Autowired
    private CustomPaperMapper paperMapper;

    @Autowired
    private QuestionOptionMapper questionOptionMapper;

    @Autowired
    private TeacherService teacherService;

    @GlobalTransactional
    @Override
    public int insert(Paper paper) {
        setPaper(paper);
        int rows = paperMapper.insertSelective(paper);
        teacherService.incrPaperCount(paper.getTeacherId() + "");
        return rows;
    }

    private void setPaper(Paper paper) {
        paper.setPaperDate(new Date());
        Teacher teacher = getTeacher(paper);
        paper.setTeacherName(teacher.getTeacherName());
    }

    private Teacher getTeacher(Paper paper) {
        ResultVo resultVo = teacherService.selectOne(paper.getTeacherId() + "");
        Map<String, Object> map = (Map<String, Object>) resultVo.getData();
        return copyMap(map);
    }

    private static Teacher copyMap(Map map) {
        Teacher teacher = new Teacher();
        // 使用BeanUtils将map中的数据封装到teacher对象中
        try {
            BeanUtils.populate(teacher, map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return teacher;
    }

    @Override
    public int delete(Integer paperId) {
        return paperMapper.deleteByPrimaryKey(paperId);
    }

    @Override
    public int update(Paper paper) {
        setPaper(paper);
        return paperMapper.updateByPrimaryKeySelective(paper);
    }

    @Override
    public Paper selectOne(Integer paperId) {
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        if (paper == null) {
            throw new RuntimeException("paper is null, paperId=" + paperId);
        }
        return paper;
    }

    @Override
    public List<Paper> selectAll() {
        return paperMapper.selectByExample(null);
    }

    @Override
    public List<Paper> selectByStatus(Integer status) {
        PaperExample example = new PaperExample();
        example.or().andPaperStatusEqualTo(status);
        return paperMapper.selectByExample(example);
    }

    @Override
    public int distributeQuestion(Integer paperId, List<Integer> questionIds) {
        paperMapper.deletePaperQuestionByPaperId(paperId);
        return paperMapper.distributeQuestion(paperId, questionIds);
    }

    @Override
    public Paper selectPaperQuestionByPaperId(Integer paperId) {
        return paperMapper.selectPaperQuestionByPaperId(paperId);
    }

    @Override
    public Paper selectPaperQuestionOptionByPaperId(Integer paperId) {
        Paper paper = this.selectPaperQuestionByPaperId(paperId);
        List<Question> questions = paper.getQuestions();
        for (Question question : questions) {
            Integer questionId = question.getQuestionId();
            QuestionOptionExample example = new QuestionOptionExample();
            example.or().andQuestionIdEqualTo(questionId);
            List<QuestionOption> questionOptions = questionOptionMapper.selectByExample(example);
            question.setQuestionOptions(questionOptions);
        }
        return paper;
    }

    @Override
    public int deletePaperQuestionByPaperId(Integer paperId) {
        return paperMapper.deletePaperQuestionByPaperId(paperId);
    }

}
